package net.cardgame.orcalecard;

import java.util.ArrayList;

import jp.jma.oraclecard.R;
import net.cardgame.oraclecard.common.onDeleteNewsBeanListener;
import net.cardgame.orcalecard.bean.NewsBean;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

class NewsBeanAdapter extends ArrayAdapter<NewsBean> implements
		OnTouchListener, OnClickListener {
	ArrayList<NewsBean> listNewsBean = new ArrayList<NewsBean>();
	Activity context;
	boolean ontouch;
	int x_old;
	int selectedId = 0;
	onDeleteNewsBeanListener listener;

	public NewsBeanAdapter(Activity context, ArrayList<NewsBean> objects) {
		super(context, R.layout.item_listview_newsbean, objects);
		// TODO Auto-generated constructor stub
		listener = (onDeleteNewsBeanListener) context;
		this.listNewsBean = objects;
		this.context = context;
	}

	static class ViewHolder {
		TextView txtMemory;
		TextView txtDatetime;
		ImageView btnDelete;
	}

	public void addNewsBean(NewsBean newsbean) {
		listNewsBean.add(newsbean);
		this.notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View rowView = convertView;
		if (rowView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			rowView = inflater.inflate(R.layout.item_listview_newsbean, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.txtMemory = (TextView) rowView
					.findViewById(R.id.txtmemory_item_listnewsbean);
			viewHolder.txtDatetime = (TextView) rowView
					.findViewById(R.id.txtDatetime_edit_history);
			viewHolder.btnDelete = (ImageView) rowView
					.findViewById(R.id.btnDelete_edit_history);
			rowView.setTag(viewHolder);
		}
		ViewHolder holder = (ViewHolder) rowView.getTag();
		NewsBean newsbean = listNewsBean.get(position);
		holder.txtMemory.setText(newsbean.sumary);
		holder.txtDatetime.setText(newsbean.datetime);
		holder.btnDelete.setVisibility(View.INVISIBLE);
		rowView.setId(newsbean.id);
		rowView.setOnTouchListener(this);
		return rowView;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		for (NewsBean newsBean : listNewsBean) {
			if (newsBean.id == selectedId) {
				// delete NewsBean
				listener.onDeleted(newsBean);
				listNewsBean.remove(newsBean);
				notifyDataSetChanged();
				return;
			}
		}
	}

	@SuppressLint("ResourceAsColor")
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			x_old = (int) event.getRawX();
			if (ontouch) {
				selectedId = 0;
				notifyDataSetChanged();
				ontouch = false;
				return false;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			int x = (int) event.getRawX();
			if (Math.abs(x - x_old) > 5) {
				ontouch = true;
				ImageView btnDelete = (ImageView) v
						.findViewById(R.id.btnDelete_edit_history);
				btnDelete.setVisibility(View.VISIBLE);
				selectedId = v.getId();
				btnDelete.setOnClickListener(this);
				ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 1.0f,
						1.0f, Animation.RELATIVE_TO_SELF, 1.0f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				anim.setDuration(500);
				btnDelete.startAnimation(anim);
			}
			break;
		case MotionEvent.ACTION_UP:
			break;
		default:
			break;
		}
		return true;
	}
}